<template>
	<view class="body">
		<view class="">
			<u-image width="100%" height="400rpx" :src="xiangq.cover_url"></u-image>
		</view>
		
			<view class="title">
				{{xiangq.title}}
			</view>
			
			<view class="herd">
				<view class="jiage">
					售价:{{xiangq.price}}
				</view>
				<view class="xiaoliang">
					销量:{{xiangq.sales}}
				</view>
			</view>
			<u-tabs-swiper bar-width="100" ref="tabs" :list="paixuList" :is-scroll="false" @change="change" :current="current"></u-tabs-swiper>
			<view class="xq">
				
				<view class="u-p-b-80" v-if="current==0">
					<u-parse :html="xiangq.details"></u-parse>
				</view>
				
				<view class="u-p-b-80" v-if="current==1">
					<view class="comment" v-for="(res, index) in commentList" :key="res.id">
						<view class="left"><image :src="res.user.avatar_url" mode="aspectFill"></image></view>
						<view class="right">
							<view class="top">
								<view class="name">{{ res.user.name}}</view>
							
							</view>
							<view class="content">{{ res.content }}</view>
						
							<view class="bottom">
								{{ res.updated_at }}
								
							</view>
						</view>
					</view>
				</view>
				
				<view class="u-p-b-80" v-if="current==2">
					
					<u-row gutter="16">
								<u-col span="6" v-for="itme in tuijian.length!==0 ? tuijian :[{},{},{},{}]">
									
									<goods-card :itme="itme"></goods-card>
										
								</u-col>
								
							</u-row>
				</view>
				
			</view>
			<view class="yejiao">
				
			
			<view class="navigation" style=" position: fixed;bottom: 0; width: 100%; display: flex; justify-content: center;">
				<view class="left">
					
					<view class="item u-text-center" @click="shouc" >
						<block v-if="ztm===1">
						<u-icon name="home" :size="40" :color="$u.color['contentColor']" color="red"></u-icon>
						<view class="text u-line-1"  style="color: red;">已收藏</view>
						</block>
						<block v-if="ztm===0">
						<u-icon name="home" :size="40" :color="$u.color['contentColor']" style="color: black;"></u-icon>
						<view class="text u-line-1"  >收藏</view>
						</block>
					</view>
					<view class="item car"  @click="togwc">
						<u-badge class="car-num" :count="gwcsl" type="error" :offset="[-3, -6]"></u-badge>
						<u-icon name="shopping-cart" :size="40" :color="$u.color['contentColor']"></u-icon>
						<view class="text u-line-1">购物车</view>
					</view>
				</view>
				<view class="right">
					<view class="cart btn u-line-1" @click="gwc">加入购物车</view>
					<view class="buy btn u-line-1">立即购买</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				commentList:[],
				
				xq:'商品详情页',
				
				
				
				paixuList:[
					{name:"商品详情"},
					{name:"商品评论",count:0},
					{name:"推荐商品"},
					
					
					
				],
				current:0,
				
				title:'《2021性爱技巧》',
				jiage:'999',
				xiaoliang:'999',
				 list : [
					'https://oss.shop.eduwork.cn/uWGocFVuhZQwwA8IFxR7.jpg',
					
				],
				id:null,
				xiangq:[],
				tuijian:[],
				pl:[],
				ztm:0,
				gwcsl:0,
				
			}
		},
		onLoad(option) {
		// console.log(option)
		this.id = option.id
		this.getData()//商品数据
		this.getshuliang()//购物车数据
		
		},
		
		methods: {
			change(index) {
				this.current = index;
				if(index==0){
					this.xq = '商品详情页'
				}else if(index==1){
					this.xq = '商品评论页'
				}else if(index==2){
					this.xq = '商品推荐页'
				}
				
				
						
			},
			//获取数据的方法
			async getData(){
				const res = await this.$u.api.xq(this.id)
				//console.log(res)
				this.xiangq = res.goods
				this.tuijian = res.like_goods
				this.commentList = res.goods.comments
				// 获取评论数量
				this.paixuList[1].count = res.goods.comments.length
				this.ztm = res.goods.is_collect
				
					
	
			},
			//请求收藏的方法
			async shouc(){
				await this.$u.api.sc(this.id)
				if(this.ztm===0){
					this.$u.toast('收藏成功')
					
					this.ztm = 1
				}else{
					this.$u.toast('取消成功')
					this.ztm = 0
				}
				
				
				
			},
			async gwc(){
				const params = {goods_id: this.id}
				await this.$u.api.gwc(params)
				
				// 重新获取购物车数据
				this.getshuliang()
				this.$u.toast('添加购物车成功')
			},
		async getshuliang (){
					
					
						const res = await this.$u.api.gwclb()
						console.log(res)
						this.gwcsl = res.data.length
					
					
				},
				//跳转到购物车页面
				togwc(){
					this.$u.route({
						type:'switchTab',
						url:'/pages/gwc/index'
					})	
				}
				
			}
		}
	
</script>

<style lang="scss" scoped>
	.title{
		margin-top: 30rpx;
		font-size: 18px;
		margin-left: 10rpx;
		
	}
	.herd{
		margin-bottom: 20rpx;
		margin-top: 30rpx;
		display: flex;
		
		
	}
	.jiage{
		margin-left: 60rpx;
		color: red;
		font-size: 16px;
	}
	.xiaoliang{
		 text-align: right;
		 margin-left:auto;
		 margin-right: 60rpx;
		 font-size: 16px;
	}
	.comment {
		display: flex;
		padding: 30rpx;
		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}
		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;
				.name {
					color: #5677fc;
				}
				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;
					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}
				.highlight {
					color: #5677fc;
					.num {
						color: #5677fc;
					}
				}
			}
			.content {
				margin-bottom: 10rpx;
			}
			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;
				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;
					.username {
						font-size: 24rpx;
						color: #999999;
					}
				}
				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;
					.more {
						margin-left: 6rpx;
					}
				}
			}
			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
	.navigation {
		
		 display: flex;
		
		margin-top: 200rpx;
		border: solid 2rpx #f2f2f2;
		background-color: #ffffff;
		padding: 16rpx 0;
		.left {
			display: flex;
			font-size: 20rpx;
			.item {
				margin: 0 30rpx;
				&.car {
					text-align: center;
					position: relative;
					.car-num {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
					}
				}
			}
		}
		.right {
			display: flex;
			font-size: 28rpx;
			align-items: center;
			.btn {
				line-height: 66rpx;
				padding: 0 30rpx;
				border-radius: 36rpx;
				color: #ffffff;
			}
			.cart {
				background-color: #ed3f14;
				margin-right: 30rpx;
			}
			.buy {
				background-color: #ff7900;
			}
		}
	}
	
	.yejiao{
		position:fixed;
		z-index:999;
	}

</style>
